﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace wpdotnet
{
    public class Config
    {
        public static WPConfiguration WPConfig
        {
            get
            {
                if (_wpConfig == null)
                {
                    _wpConfig = ConfigurationManager.GetSection("wpdotnet/wpconfiguration") as WPConfiguration;
                }
                return _wpConfig;
            }
        }
        private static WPConfiguration _wpConfig = null;

        public static DataBaseConfiguration WPDataConfig
        {
            get
            {
                if (_wpDataConfig == null)
                {
                    _wpDataConfig = ConfigurationManager.GetSection("wpdotnet/wpdatabase") as DataBaseConfiguration;
                }
                return _wpDataConfig;
            }
        }
        private static DataBaseConfiguration _wpDataConfig = null;

        public static WPSecurityConfiguration WPSecurity
        {
            get
            {
                if (_wpSecurity == null)
                {
                    _wpSecurity = ConfigurationManager.GetSection("wpdotnet/wpsecurity") as WPSecurityConfiguration;
                }
                return _wpSecurity;
            }
        }
        private static WPSecurityConfiguration _wpSecurity = null;
    }

    public class WPConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("InstallDir")]
        public string InstallDir
        {
            get
            {
                return Convert.ToString(this["InstallDir"] ?? "~/blog");
            }
            set
            {
                this["InstallDir"] = value;
            }
        }


        [ConfigurationProperty("UseWPConfigFile")]
        public bool UseWPConfigFile
        {
            get
            {
                return Convert.ToBoolean(this["UseWPConfigFile"] ?? "true");
            }
            set
            {
                this["UseWPConfigFile"] = value;
            }
        }

        [ConfigurationProperty("TestMode")]
        public bool TestMode
        {
            get
            {
                return Convert.ToBoolean(this["TestMode"] ?? "false");
            }
            set
            {
                this["TestMode"] = value;
            }
        }
        
    }

    public class DataBaseConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("ConnectionString")]
        public string ConnectionString
        {
            get
            {
                return Convert.ToString(this["ConnectionString"]);
            }
            set
            {
                this["ConnectionString"] = value;
            }
        }

        [ConfigurationProperty("TablePrefix")]
        public string TablePrefix
        {
            get
            {
                return Convert.ToString(this["TablePrefix"] ?? "wp_");
            }
            set
            {
                this["TablePrefix"] = value;
            }
        }
    }

    public class WPSecurityConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("CacheKeyPrefix")]
        public string CacheKeyPrefix
        {
            get
            {
                return Convert.ToString(this["CacheKeyPrefix"] ?? "WPDOTNETCACHE_");
            }
            set
            {
                this["CacheKeyPrefix"] = value;
            }
        }


        [ConfigurationProperty("IsCacheEnabled")]
        public bool IsCacheEnabled
        {
            get
            {
                return Convert.ToBoolean(this["IsCacheEnabled"] ?? "false");
            }
            set
            {
                this["IsCacheEnabled"] = value;
            }
        }
    }
}
